#导入工具库
import os
import io
import sklearn
import numpy as np
from sklearn.cluster import KMeans
from sklearn.datasets import load_boston
import json
import sys

#这里采用json
def loadData(filePath):
    file=open(filePath,"r+",encoding='UTF-8')
    content=file.read()
    file.close()
    jsonData=json.loads(content)
    # outType(content)
    # outType(jsonData)
    retData=[]
    retCityName=[]
    for item in jsonData:
        retData.append(item["amount"])
        retCityName.append(item["city"])
    return retData,retCityName

def outType(clz):
    print(type(clz))

if __name__=='__main__':
    data,cityName=loadData("static/city.json")

    km=KMeans(n_clusters=3)
    data=[[i] for i in data]
    # print("data",data)
    # print("cityName",cityName)
    lable=km.fit_predict(data)
    expenses=np.sum(km.cluster_centers_,axis=1)
    CityCluster=[[],[],[]]
    for i in range(len(cityName)):
        CityCluster[lable[i]].append(cityName[i])
    for i in range(len(CityCluster)):
        print("Expenses:%.2f" % expenses[i])
        print(CityCluster[i])

